package com.mxchip.livestar;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.facebook.stetho.Stetho;
import com.google.code.microlog4android.config.PropertyConfigurator;
import com.mxchip.livestar.base.AppConstant;
import com.mxchip.livestar.repository.RoomClientRepository;
import com.mxchip.livestar.ui.call.AVDevice;
import com.orhanobut.hawk.Hawk;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.videohigh.graham.Settings;


public class Appli extends Application {


    // 创建服务用于捕获崩溃异常
    private Thread.UncaughtExceptionHandler restartHandler = (thread, ex) -> {
        restartApp();//发生崩溃异常时,重启应用
    };

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        /**
         * 注意: 即使您已经在AndroidManifest.xml中配置过appkey和channel值，也需要在App代码中调
         * 用初始化接口（如需要使用AndroidManifest.xml中配置好的appkey和channel值，
         * UMConfigure.init调用中appkey和channel参数请置为null）。
         */
        UMConfigure.init(this, "5d64a5ed4ca357dd9e0005c5", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL);
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        RoomClientRepository.INSTANCE.destroy();
    }

    private void init() {
        Hawk.init(getApplicationContext()).build();

        PropertyConfigurator.getConfigurator(this).configure();
        Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程  以下用来捕获程序崩溃异常
        CrashReport.initCrashReport(getApplicationContext());
        Stetho.initializeWithDefaults(this);


        RoomClientRepository.INSTANCE.setLogEventListener((tag, message) -> Log.i("graham", tag + " " + message));

        RoomClientRepository.INSTANCE.init(getApplicationContext(), new Settings(
                AppConstant.LUXX_ACCESS_ID, AppConstant.LUXX_ACCESS_SECRET,
                new String[]{ AVDevice.CAMERA_LOCAL, AVDevice.CAMERA_LOCAL_SMALL },
                new String[]{ AVDevice.CAMERA_HDMI, AVDevice.CAMERA_WIFI })
        );
//        new ANRWatchDog().start();

         /*Rest rest = new Rest(AppConstant.LUXX_ACCESS_ID, AppConstant.LUXX_ACCESS_SECRET);

         rest.getDeviceToken(SystemData.deviceId(), 3600, new RespCallback<TokenResp>() {
             @Override
             public void onError(int code, String message) {
                 Log.e("Appli", "code " + code + ", message " + message);
             }

             @Override
             public void onRespData(TokenResp data) {
                 Log.e("Appli", "data " + data);

                 rest.verifyDevice(data.getToken(), new RespCallback<TermDeviceResp>() {
                     @Override
                     public void onError(int code, String message) {
                         Log.e("Appli", "code " + code + ", message " + message);
                     }

                     @Override
                     public void onRespData(TermDeviceResp termDeviceResp) {
                         Log.e("Appli", "data " + termDeviceResp);
                     }
                 });
             }
         });*/
    }

    public void restartApp() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
    }

}
